package com.vanke.gankclient.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.webkit.WebView
import android.widget.AbsoluteLayout
import android.widget.ProgressBar
import com.vanke.gankclient.R

/**
 * @name GankClient
 * @package name：com.vanke.gankclient.view
 * @class describe
 * @anthor shiyuchuan
 * @time 2017/8/10 15:50
 */
class ProgressWebView(context: Context, attributeSet: AttributeSet) : WebView(context, attributeSet) {

    private val progressBar: ProgressBar

    init {
        progressBar = ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal)
        progressBar.layoutParams = AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT
                , 10, 0, 0)
        val drawable = context.resources.getDrawable(R.drawable.progress_bar)
        progressBar.progressDrawable = drawable
        addView(progressBar)
        webChromeClient = WebChromeClient()
        settings.builtInZoomControls = true
    }

    inner class WebChromeClient : android.webkit.WebChromeClient(){

        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            if (newProgress == 100){
                progressBar.visibility = View.GONE
            }else{
                if (progressBar.visibility == View.GONE)
                progressBar.visibility = View.VISIBLE
                progressBar.progress = newProgress
            }
            super.onProgressChanged(view, newProgress)
        }
    }

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
        val lp = progressBar.layoutParams as AbsoluteLayout.LayoutParams
        lp.x = l
        lp.y = t
        progressBar.layoutParams = lp
        super.onScrollChanged(l, t, oldl, oldt)
    }
}